class ReviewType < ActiveRecord::Base
  attr_accessor :parsing_error
  has_many :rating_criterions, :inverse_of => :review_type,:dependent => :destroy
  has_many :reviews, :dependent => :destroy
  validate :validate_parsing_error

  def parsing_error?
    not parsing_error.nil?
  end

  def validate_parsing_error
    if(parsing_error?)
      errors.add(:jucm_file, parsing_error)
    end
  end

  def overall_rating_criterion
    rating_criterions.find{|rating_criterion|
      rating_criterion.overall?
    }
  end

  def custom_rating_criterions
    rating_criterions.select{|rating_criterion|
      not rating_criterion.overall?
    }
  end
  
  def load_kpis
    @kpi_dal=KpiDal.new(self,num_of_reviews)
  end
  
  def num_of_reviews
    reviews.count
  end

  def get_avg_rating(rating_criterion)
    @kpi_dal.get_avg_rating(rating_criterion)
  end
  
  def get_percent_of_ratings(rating_criterion,rating_scale_element)
    @kpi_dal.get_percent_of_ratings(rating_criterion,rating_scale_element)
  end

  def get_num_of_ratings(rating_criterion,rating_scale_element)
    @kpi_dal.get_num_of_ratings(rating_criterion,rating_scale_element)
  end
end
